package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.entity.Lat_Lng;
import com.sky.exception.BaseException;
import com.sky.mapper.AddressBookMapper;
import com.sky.properties.AddressProperties;
import com.sky.service.AddressBookService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import com.sky.utils.SearchHttpSN;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    AddressProperties addressProperties;
    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.save(addressBook);
    }

    @Override
    public List<AddressBook> list() {
        AddressBook addressBook = AddressBook.builder().userId(BaseContext.getCurrentId()).build();
        return addressBookMapper.list(addressBook);
    }

    @Override
    public AddressBook getDefault() {
        SearchHttpSN snCal = new com.sky.utils.SearchHttpSN();
        AddressBook addressBook = AddressBook.builder().userId(BaseContext.getCurrentId()).isDefault(1).build();
        List<AddressBook> list = addressBookMapper.list(addressBook);
        String Full_address = list.get(0).getProvinceName() + list.get(0).getCityName() + list.get(0).getDistrictName() + list.get(0).getDetail();
        try {
            String useraddress = snCal.useraddress(Full_address);
            Lat_Lng dismantle = dismantle(useraddress);
            String businessaddress = snCal.businessaddress();
            Lat_Lng dismantle1 = dismantle(businessaddress);
            double distance = dismantle.distance(dismantle1);
            if (distance >5.0){
                throw new Exception("距离太远");
            }
        } catch (Exception e) {
            throw new BaseException("距离太远");
        }

        return list.get(0);
    }

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = AddressBook.builder().id(id).build();
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list.get(0);
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void setDefault(Integer id) {
        //先将该用户所有的地址设置为非默认地址
        addressBookMapper.setAllNotDefault(BaseContext.getCurrentId());
        //再将该id的地址设为默认
        AddressBook addressBook = AddressBook.builder().id(id.longValue()).isDefault(1).build();
        addressBookMapper.update(addressBook);
    }



    public static Lat_Lng dismantle(String input){
        // 1. 定义正则表达式（匹配 lng 和 lat）
        Pattern pattern = Pattern.compile("\"lng\":\\s*(-?\\d+\\.\\d+),\\s*\"lat\":\\s*(-?\\d+\\.\\d+)");
        Matcher matcher = pattern.matcher(input);

        // 2. 提取经纬度
        if (matcher.find()) {
            double lng = Double.parseDouble(matcher.group(1));
            double lat = Double.parseDouble(matcher.group(2));
            System.out.println("经度 (lng): " + lng);
            System.out.println("纬度 (lat): " + lat);
            return new Lat_Lng(lat,lng);
        } else {
            System.out.println("未找到经纬度！");
            return null;
        }
    }
}
